var productName = "";
$(document).ready(function () {
    var nowToShare = window.location.search.indexOf("nowToShare");
    if(nowToShare >=0 ) {
        $(".success-text").text("去请客分享咖啡");
    }
    wx.ready(function () {
        var orderId = getPageCode("order_id");
        var orderQuery = "order/query";
        var orderQueryParams = {
            "SESSION_ID": SESSION_ID,
            "ORDER_ID": orderId
        }
        request(orderQuery, orderQueryParams, function (res) {
            // alert(JSON.stringify(res));
            var flg = /^0+$/ig.test(res.RSPCD); //全零true
            if(!flg) return showErroMsg("订单异常，无法分享！",function () {
                window.location.replace("./index.html");
            },true);
            var SKU_ID = res.BODY.STORE_ORDER[0].SKU_LIST[0].SKU_ID;
            productName = res.BODY.STORE_ORDER[0].SKU_LIST[0].SKU_NAME;
            var createSharePath = "order/share/create";
            var createShareParams = {
                "STORE_ID": STORE_ID,
                "ORDER_ID": orderId,
                "SKU_ID": SKU_ID
            };
            request(createSharePath, createShareParams, function (res) {
                // alert(JSON.stringify(res));

                var flg = /^0+$/ig.test(res.RSPCD); //全零true
                if(!flg) return showErroMsg("订单异常无法创建分享！",function () {

                },true);
                var SHARE_ID = res.BODY.SHARE_ID;
                var shareJson = {
                    "SHARE_ID":SHARE_ID,
                    "productName":productName,
                    "STORE_ID":STORE_ID
                };
                var shareParams = "";
                // if(IS_BIND) {
                //     shareParams = encodeURI(encodeURI( 'http://wechat.miniffee.com/coffee/html/html/receive_success_toUse.html?share_coupon='+JSON.stringify(shareJson)));
                // } else {
                    shareParams = encodeURI(encodeURI( 'http://wechat.miniffee.com/coffee/html/register.html?share_coupon='+JSON.stringify(shareJson)));
                // }
                // alert(shareParams);
                var params = {
                    "SESSION_ID":SESSION_ID
                }
                request("user/info/get",params,function (res) {
                    wx.onMenuShareAppMessage({
                        title: '哇！'+(res.BODY.USER_NAME ? res.BODY.USER_NAME:"未知") +'请你喝咖啡', // 分享标题
                        desc: '小啡办公室自助咖啡吧，为你提供新鲜烘焙的、现磨的牛奶咖啡，点击领取分享劵...', // 分享描述
                        link: shareParams, // 分享链接，该链接域名必须与当前企业的可信域名一致
                        imgUrl: 'http://wechat.miniffee.com/coffee/assets/img/img_summer_card.png', // 分享图标
                        type: '', // 分享类型,music、video或link，不填默认为link
                        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                        success: function (res) {
                            // 用户确认分享后执行的回调函数
                            location.replace("./share_success.html");
                        },
                        cancel: function (res) {
                            // 用户取消分享后执行的回调函数
                        },
                        faild:function (res) {
                        },
                        complete:function (res) {
                        }
                    })
                })
            })
        })
    });
})

